package com.apanal.qlife.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.sys.model.Dictionary;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.DictionaryService;

/**
 * 数据字典控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-21下午5:20:44
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

	@Autowired
	private DictionaryService service;

	/**
	 * 分页查询
	 * 
	 * @param dictionary
	 * @param page
	 * @param model
	 * @return
	 */
	@SystemLog(module = "数据字典管理", operation = "分页查询数据字典", type = LogType.SELECT)
	@RequiresPermissions("sys:dictionary:view")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Dictionary dictionary, Page page, Model model) {
		dictionary.setLanguage(Constants.LANGUAGE_ZH_CN);
		PageReturn<Dictionary> result = service.selectForPage(dictionary, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "sys/dictionaryList";
	}

	/**
	 * 转向新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:dictionary:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toCreate(Model model) {
		model.addAttribute("dictionary", new Dictionary());
		model.addAttribute("op", "新增数据字典");
		return "sys/dictionaryEdit";
	}

	/**
	 * 新增
	 * 
	 * @param dictionary
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "数据字典管理", operation = "新增数据字典", type = LogType.CREATE)
	@RequiresPermissions("sys:dictionary:create")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@CurrentUser User user, Dictionary dictionary) {
		dictionary.setInsertBy(user.getUseId());
		dictionary.setLanguage(Constants.LANGUAGE_ZH_CN);
		dictionary.setpCode(0);
		dictionary.setpCodes("0/");
		if (dictionary.getShowIndex() == null) {
			dictionary.setShowIndex(1);
		}
		service.insert(dictionary);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 转向更新页面
	 * 
	 * @param idId
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:dictionary:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("id") Long id, Model model) {
		model.addAttribute("dictionary", service.selectById(id));
		model.addAttribute("op", "修改数据字典");
		return "sys/dictionaryEdit";
	}

	/**
	 * 更新
	 * 
	 * @param dictionary
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "数据字典管理", operation = "修改数据字典", type = LogType.UPDATE)
	@RequiresPermissions("sys:dictionary:update")
	@ResponseBody
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@CurrentUser User user, Dictionary dictionary) {
		dictionary.setLastUpdateBy(user.getUseId());
		service.updateByIdSelective(dictionary);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:dictionary:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("dictionary", service.selectById(id));
		model.addAttribute("op", "查看数据字典");
		return "sys/dictionaryDetail";
	}

	/**
	 * 硬删除
	 * 
	 * @param user
	 * @param id
	 * @return
	 */
	@SystemLog(module = "数据字典管理", operation = "删除数据字典", type = LogType.DELETE)
	@RequiresPermissions("sys:dictionary:delete")
	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		service.deleteById(id);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 启用/停用
	 * 
	 * @param user
	 * @param dictionary
	 * @return
	 */
	@SystemLog(module = "数据字典管理", operation = "启用/停用数据字典", type = LogType.UPDATE)
	@RequiresPermissions("sys:dictionary:start/stop")
	@ResponseBody
	@RequestMapping(value = "/{id}/{normal}/updateToEnableOrDisable", method = RequestMethod.GET)
	public String updateToEnableOrDisable(@CurrentUser User user,
			Dictionary dictionary) {
		dictionary.setLastUpdateBy(user.getUseId());
		service.updateToEnableOrDisable(dictionary);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 检测是否符合唯一索引
	 * 
	 * @param code
	 * @param type
	 * @param language
	 * @return true:符合 false:不符合
	 */
	@ResponseBody
	@RequestMapping(value = "/{id}/{code}/{type}/checkUniqueIndex", method = RequestMethod.GET)
	public boolean checkUniqueIndex(@PathVariable("id") Long id,
			@PathVariable("code") Integer code,
			@PathVariable("type") String type) {
		return service.checkUniqueIndex(id, code, type,
				Constants.LANGUAGE_ZH_CN);
	}

}
